﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNOgre.Graphics;
using XNOgre.Scene;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace XNOgre.Core
{
    public sealed class Root : IServiceProvider
    {
        #region Static Properties/Methods
        private static Dictionary<Type, int> namesGenerated = new Dictionary<Type, int>();
        public static string GenerateName(Type type)
        {
            string name = null;

            if (namesGenerated.ContainsKey(type))
            {
                int currentCount = namesGenerated[type];
                currentCount++;
                name = "Unnamed_" + type.Name + "_" + currentCount;
                namesGenerated[type] = currentCount;
            }
            else
            {
                name = "Unnamed_" + type.Name + "_" + "0";
                namesGenerated.Add(type, 0);
            }
            return name;
        }

        private static Root _instance = null;
        public static Root Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Root(true);
                }
                return _instance;
            }
        }
        #endregion

        public ContentManager Content { get; set; }
        public RenderWindow RenderWindow { get; private set; }
        public RenderSystem RenderSystem { get; private set; }

        private bool keepLog;

        public Root(bool log)
        {
            this.keepLog = log;
            _instance = this;
        }

        public void Log(string message)
        {
            if (keepLog)
            {
                Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "]" + ": " + message);
            }
        }
        public void InitRenderSystem(GraphicsDevice graphicsDevice)
        {
            RenderSystem = new RenderSystem(graphicsDevice);
        }
        public SceneManager CreateScene(string name = null)
        {
            SceneManager scene = new SceneManager(name);
            return scene;
        }
        public RenderWindow CreateRenderWindow(Microsoft.Xna.Framework.GameWindow gameWindow)
        {
            RenderWindow = new RenderWindow(gameWindow);
            return RenderWindow;
        }
        public void Render()
        {
            if (RenderWindow == null)
            {
                Log("Attempt at using null Window, call CreateRenderWindow in Initialize()");
                return;
            }

            RenderWindow.Render();
        }
        public object GetService(Type serviceType)
        {
            return null;
        }
    }
}
